﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.Api.Biz
{
    public class HuodongService : ApiService
    {
        /// <summary>
        /// 获得活动信息
        /// </summary>
        /// <returns></returns>
        public void getShare(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long seq_id = dic.GetValue<long>("seq_id", 0);
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var aDZ_HuodongShare = dbcontext.ADZ_HuodongShare.FirstOrDefault(t => t.seq_id == seq_id);
            if (aDZ_HuodongShare != null)
            {
                long huodong_id = aDZ_HuodongShare.huodong_id;

                var query_params_dic = JsonHelper.Deserialize<Dictionary<string, string>>(aDZ_HuodongShare.query_params);

                string share_title;
                string no_store;        //0 不转发缺货 1 全部转发
                string add_rate;        //加价幅度，大于0的整数
                string base_rate;

                query_params_dic.TryGetValue("share_title", out share_title);
                query_params_dic.TryGetValue("no_store", out no_store);
                query_params_dic.TryGetValue("add_rate", out add_rate);
                query_params_dic.TryGetValue("base_rate", out base_rate);


                ShareGoodsListModel shareGoodsListModel = new ShareGoodsListModel();
                shareGoodsListModel.share_title = share_title;

                var show_rate = ConvertHelper.ConvertTo<decimal>(add_rate, 0) + ConvertHelper.ConvertTo<decimal>(base_rate, 0);

                Tuple<bool, GoodsListModel> tupleResult = HuodongBiz.Instance.GetGoodsList(apiContext.CurrUserId, huodong_id, ConvertHelper.ConvertTo<int>(no_store, 0), pageindex, pagerows,1);
                if (tupleResult.Item1)
                {

                    if (tupleResult.Item2 != null && tupleResult.Item2.list != null)
                    {
                        tupleResult.Item2.list.ForEach(t =>
                        {
                            t.price = (1 + show_rate) * t.price;
                        });
                    }
                    shareGoodsListModel.goodsList = tupleResult.Item2;

                    apiResponse.retCode = ResultEnum.Success;
                    apiResponse.data = JsonHelper.Serialize(shareGoodsListModel);
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            apiResponse.retCode = ResultEnum.NoData;
            apiResponse.message = "商品为空";
        }
        /// <summary>
        /// 活动转发
        /// </summary>
        /// <returns></returns>
        public void Share(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int huodong_id = dic.GetValue<int>("huodong_id", 0);
            string share_title = dic.GetValue("share_title", "");
            int no_store = dic.GetValue<int>("no_store", 0);        //0 不转发缺货 1 全部转发
            decimal add_rate = dic.GetValue<decimal>("add_rate", 0);        //4位小数

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var aDZ_HuodongShare = new ADZ_HuodongShare();
            aDZ_HuodongShare.uid = apiContext.CurrUserId;
            aDZ_HuodongShare.huodong_id = huodong_id;

            var my_level = dbcontext.ADZ_MemberMaster.First(t => t.uid == apiContext.CurrUserId).member_level;
            var member_config = dbcontext.ADZ_MemberLevelConfig.FirstOrDefault(t => t.member_level == my_level);
            var base_rate = member_config.add_rate;

            var query_params_obj = new
            {
                share_title,
                no_store,
                add_rate = add_rate,//转发时设置的比例
                base_rate = base_rate//用户会员等级对应的加价比例
            };
            aDZ_HuodongShare.create_time = DateTime.Now;
            aDZ_HuodongShare.query_params = JsonHelper.Serialize(query_params_obj);
            dbcontext.ADZ_HuodongShare.Add(aDZ_HuodongShare);
            dbcontext.SaveChanges();

            if (aDZ_HuodongShare.seq_id > 0)
            {
                apiResponse.retCode = ResultEnum.Success;
                var retdata = new
                {
                    url = HuodongBiz.Instance.getShareUrl(aDZ_HuodongShare.seq_id)
                };
                apiResponse.data = JsonHelper.Serialize(retdata);
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = "转发地址生成失败";
            }
            apiContext.apiHttpResp = apiResponse;
        }
        /// <summary>
        /// 活动列表查询
        /// </summary>
        /// <returns></returns>
        public void GetList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            //int label_type = dic.GetValue<int>("label_type", -10);      //4 竞价 5 活动
            //long brand_id = dic.GetValue<long>("brand_id", -10);        //品牌id
            //long category_id = dic.GetValue<long>("category_id", -10);  //分类id
            //string selKey = dic.GetValue<string>("selkey");             //搜索关键字
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);



            Tuple<bool, HuodongListModel> tupleResult = HuodongBiz.Instance.GetList(apiContext.CurrUserId, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "活动列表为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取活动信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long huodong_id = dic.GetValue<int>("huodong_id", -10);
            long uid = apiContext.CurrUserId;
            var tupleResult = HuodongBiz.Instance.GetInfo(uid, huodong_id);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "活动信息为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 活动商品列表
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetGoodsList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long huodong_id = dic.GetValue<int>("huodong_id", -10);
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            Tuple<bool, GoodsListModel> tupleResult = HuodongBiz.Instance.GetGoodsList(apiContext.CurrUserId, huodong_id, 1, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "商品为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

    }
}